refactoring to smaller functions