Solves problem 8, though not the best way
Simplifies the code somewhat