optimizations and error handling