589 lines of code later, solution complete