Changed the many lines of code into loops for readability's sake