cleanup and better error reporting