refactored code to stop using if statements and use exceptions instead