refactored based on Abi's code review