Refactored based on Joaquin's code review