Moved PID to a subfunction