refactored code as functions