Refactoring original larger function.