Added O365 support and some refactoring