Fixed few memory bugs