Preparation of mesylates and tosylates