slightly modified original code that came with the controller