Refactored writetodisk (thanks @gluejar)