proper fix for segfault, credit to manuel-arguelles