Corrected some logic issues