Corrected some logic bugs.