A bunch of minor code refactoring.