fixed bugs + unit tests