Fixed logic errors