Tests to clear #218

 I also introduced some refactorings in the tests along the way.