I'm about to rewrite it completely, so the first thing I need to do is to write tests for as much of the functionality as possible, so that I can check the new implementation behaves in the same ways.