Move code to print list to a function