Fixing PHP memory leak