refactor peg
divide helper code