Cleanup and error handling