write some unittests and fix errors found in the process