I won't pretend I'm proud of this code, but it works :P (fixing page order issues)