add memory management, all 4 work on sample-input1.txt