use array to improve speed; bug fixed.