Shortened code by over 70 lines