Some bugs fixed and tests added.