Refactored the code by pulling out a utility helper