Various fixes and adding tests
Fixed merge conflict