Needs some refactoring.