added unit tests and bugfixes