Refactor code, possibly fix some bugs

Refactoring mostly had to do with how errors are detected and reported.
Other changes include using more verbose variable names, and moving
repeated code together into functions.
Make -d delete multiple listed targets, atomically
Update documentation for -d, fix typos