Bugs fixed, tests added