Refactoring based on https://github.com/gongo/pecrant/pull/9