Working version but needs refactoring.