Improved error reporting, code cleanup