Minor refactoring of code. Close to pull back in master.