Abstract out duplicated code.
Merge pull request #8 from SaidinWoT/master

Factor out duplicated code/structure.