This is a work-in-progress and is mostly working like I want, but will require some refactoring to make it even more useful and less complex.