fix some bug and leaks