Partial code for a refactor that was abandoned. Going for a full rewrite now.