Improved error handling, fixed bug in setting long output