fix bug and optimum performance