changed a lot, added unit tests