Made some progress but needs heavy refactoring.