Issue 416 solution: cleanup pidfile

Resolves both #416 and #418.

Althernate solution to #422 and includes my commits from #421