Some code cleanup and optimization.