fixing bug and writing regression test