Moved functions around for program clarity