Fixed a couple of subtle bugs