fix bug and optimise