Cleanup and better error reporting