Add authorization function and fix some bugs