Code completely rewrited, optimization of the memory usage