Cleanup and improved error reporting