Fix toTempFile, added docId managing functions