Fixed error handling and improved output.