Reorganize function by grouping vars