consolidate duplicated code
Merge pull request #126 from ccd0/master

consolidate duplicated code