add memory management and improve type doc