Refactored code based on Franco and Dan's comments