some modification to prevent possible memory leaks