Improved code structure

Followed some 'idiomatic.js' principles (https://github.com/rwaldron/idiomatic.js)