fixed unit tests and bugs detected by tests