Refactor set()

Reduce code duplication in set()
Merge pull request #7 from duego/refactor-set

Refactor set()