Refactored code to use block with instance_variable_set