Refactored to eliminate superfluous array creation by using #map effectively.