Refactoring

* Use a lib instead of cramming everything into the bin/batchtapaper script
* Code style
* Lots of method extraction
* Better error handling