Fixed filehandling of dirty files to check for changes before discarding

Fixed filehandling of dirty files to check for changes before discarding