Speed improvements and code cleanup.