Fix newline mess and add an testing utility to write a log
* fix newline handling and lowering overall complexity of functions
* make jshint pass
* add utility to write test-log