Code style and algorithm enhancements