fix bug, write tests
resolve merge problems