refactored to be functional programming