fixed the logic bugs.