optimized my terrible code, get rid of redundant traverse.