Move safe_dir method into utils module