fix referencing freed memory