Improved PID code