changed program flow and added new subroutines in place of duplicated code