Moved stuff around, fixes based on scrutinizer, added unit-test