slightly more robust version, still crashes though