moved the returnURL code closer to where it is actually used