Bug fix in free function