Fixed logic bugs.