massive changes, added a restart function which writes out a temp file every couple of minutes