Fixed some logical error
Made some code cleaning