Fix horrendous performance bug and add code used to profile for bug.