Fixing bugs, adding tests.