this is in decent working shape but the code is pretty ugly. Spend some time cleaning it up at some point.