Fixing memory leak in read method