Fixed code to deal with the selective overwrite