Make this usable as a Node library

I did some minor refactoring so that it can be used programatically as a node module. The standard command line behaviour has remained unchanged.