refactored into smaller functions