Refactored into proper functions