Don't delete the file if it existed beforehand (does this leave it corrupt?)
Recoded to work with threads
Now there's a server thread!