Refactored and cleaned up.