refactoring code into smaller fns