Partial refactor. Still not great.