Bugs fixed and test added