Perform important teardown tasks first

Delete files and kill processes before doing anything else.
Simplify code