proper error handling and cleanup code