First draft. Needs refactoring.