Refactor tests

* cleaned up some code duplication in the testsuite
* minor refactor in the implementation