Fixed a few logic bugs