bugs fixed and refatoring Peg
peg optimization
adding optimization of peg