Fix if/else logic for pattern choice

In addition, overwrite existing file.