code review and optimizations