Fix memory leaks.
implement query