fix bug and improve code.