better creating of temp file to avoid race conditions and conflicts
adding latest tweaks