Solved #10, should be easy to refactor down to something more readable.