Refactored in a functional style