fixed some bugs. Unit tests