Refactored and cleaned up source.