corrected some logic