writing tests and a bit of fixing