small code refactoring over locking and pids