Some cleanup and re-factoring.