Don't have a clue about what it does, but isn't $mem defined for the first time on line 133?