added a flag to indicate read or not, fixed memory bugs