This is getting large enough that I almost feel like it should be organized as a class.