Further refactor and code cleanup.