refactor authorization function