several changes combined, including some unit tests, refactoring, bug fixes and separating working and output dir