Fix segfault upon startup

Thanks to Andrei Dziahel for this patch.

(cherry picked from commit 3e69cd212429426229e9a828a95739bf88f3d2b4)