I have been meditating over your original code. I cannot say what it does but the generalstructure looks like this: there are three sections, one for each plane. I would call that parameter $plane. In each of the three sections, there are three subsections: some prep stuff involving the two other directions in the same way and then two sections that you seem to want to refactor into doStuff. Each section needs both other directions but starts dealing with one of them first. For example, in tge z plane section it first deals with the x axis, then with the y axis. I would therefore call one direction $primary and the other $secondary. So in summary it would be doStuff( $plane, $primary, $secondary ).