MODIFIED: another round of refactor.