Added better loginas functionality and APIs for calling from elsewhere