Refactored using map, reduce, etc.