Refactored redirect code, made code more secure (?)