This is going to need a lot of clean up.  Getting close to a proof of concept though.