added some unit tests, a bit of a refactor
merge manually
merged manually