Fixing bugs (and adding test)