Bug fixed and tests added