improved recognition algorithm