removed some unnecessary code, fixed memory leak