fix NoMethodError - undefined method 'key' for nil:NilClass