Fixed logic bugs