Made redirection more dynamic based on previous page