Working but fragile version of the controller.

This still needs to be cleaned up and re-factored before making it more robust.