I've finally got a simplified version working.

Now to try to clean it up.