Fix for NilClass error

(cherry picked from commit ca00e45799525dcd3d0b6ae04ea102435667aaa5)