Implemented unit tests, minor bugfixes