Tweak template code for performance