Changed the long list of if statements to while loops because those are
a thing. Fixed the deprecated method by using a try-catch block (in
place of throwing an exception) which works.
Fixed the formatting