Better template selection for recursion

Change-Id: I8ce4c394628bf99a313ffb02242f8d73642d6c0c