Reworked things slightly to be more testable, then started on some unit tests.