memory leakage inside mahyar algorithm fixes