Shape code cleanup and optimization