Add authorization and refactor the code.