Fix file overwriting issue
The current code can in some cases overwrite capture files. This attempts to correct the problem. The issue was that it was using the multiple variables to store the target name, and it's possible to get them out of sync.
This MR also introduces code that, in case of some other bug, will try to detect overwriting files and instead increment the sequence number, if it finds one.