Reorganised the pitch code to simplify the PLC case and fixed a buffer overflow