hs: tests for the code, plus some refactoring.