Add logging and re-write the locking mechanism to use wait for PIDs