Adding tests, fixing bugs