fixed a couple of subtle but important bugs