Second attempt in trying to fix memory bug