Refactored as per @jsimnz recommendations.