Some more refactoring and added some basic tests.