fixing bugs and writing tests