added file writing and cleanup, as well as some list&dict work