Added close function and fixed pointer problem