Fixing deletd lines and optimize code