Reorganize code and remove redundancies.