pid file management fixes

Some fixes to manage the PID file properly.