Refactored to be able to be used as module

Can still be run as a CLI script using `node main.js`.