working code

moving this in from another one of my projects, I'll just clean it up
and make it more generalized