Add tempdir + refactor