optimizations and error-checking