added stock optimize code