now with unit tests
fix some bugs