Some refactoring and tidy up.
Added a bit more coverage.