Error handling and cleanup