[#3] Fix various errors caused by undefined variables.

Various errors were caused because of variables sometimes being
undefined, and this not checked for correctly.

Fix by checking that variables are defined, using `typeof`.
Merge branch 'bug/3-undefined-errors' into develop