Fix NullPointerExceptions.

Resolves issues #139 and #140.