A few new functions and code cleanup.