Refactored appending item list to output using an inner function