Refactor temp dir funcion