I'm about to butcher this.
Works and is vaguely testable.