This will likely need a major rewrite.