Implemented a better temporary file handling

This implementation is more reliable and more infrastructure friendly