Refactored to have a reusable function.