Somewhat refactored.