Cleanup and proper error handling