Refactored to functional style