This version completes the precedent commit with improved memory management.
This commits fixes all memory leaks in the actual code.