Some cleanup and code rearangement.