refactored to be more functional