This needs a lot more cleanup but at least it's a bit more complete now.