Fix some logic, add a few more tests