Vastly improved the custom prefix code,

abstracting the ugly parts into a function.