Error handling and optimizations