code refractering to condense some sections
removed the necesity for an external call to grep