Some refactoring and prepared for npm publish