create working dir if doesnt exist, and some refactoring