Bug fixed, tests added and doc updated