Refactor and major cleanup.