refactored into multiple functions