This could use a refactoring.