Fixed a few logic errors