Attempt to fix undefined method [] for nil:NilClass error