Bugs fixed and new tests