Refactored as per Abi's codereview