Think I got it

Seems to work now. Code not pretty, I think there is some random
functionality in there.