add set-redirect function to response
clean up code formatting and a little refactoring