I'm trying to construct a general circuit for Shor's algorithm in Qiskit. I understood the later parts of the circuit (inverse QFT and QPE), but can't really understand the order finding. For example, if we consider $N=15$, we have all the $\text{gcd}$ of 15 to be 2, 7, 8, 11, 13 (although I suspect that 4 is not considered as it is $2^2$). For $a=2$ or $13$, we swap qubits 0 and 1, 1 and 2, 2 and 3. If $a=7$ or $8$, we swap 2 and 3, 1 and 2, 0 and 1. If $a=11$, we swap 1 and 3, 0 and 2. Also, if $a=7, 11$ or $13$, we add an $X$ gate on all the 4 added qubits.
I want to know how we chose which qubits to swap for a particular number, and how we can generalize it, if possible.