Fixed rewriting files bug