optimizations, comments added