Refactored some repeated code, updated to work with latest version from
NPM