Refactored to functional approach and added test