Correcting some bug & Logic Optmization