Got it! Though it needs major, major, major (etc.) refactoring.