refactor into smaller functions