Bugs fixes and some new tests