refactoring to make more functional