Halfway through a massive refactor... need to save this before falling asleep.