minor improvements in performance, refactorings, comments