Add tests and some style cleanup - builds on #623