Kind of works. Needs refactoring.