Refactoring: remove unnecessary code, use efficient functions (reduce, map, apply, ...) instead of my own, rename functions