This is untested, but I can't test it until I implement other things first. I also need to do some refactoring, so I need to commit and make a checkpoint now. Consider this code as good as if didn't exist.