Refactor and minor cleanup.