The present invention relates with a process for preparing compounds of Formula I:and pharmaceutically acceptable salts thereof, wherein ring A, ring B, W, m, D, R2, R2a, R3, R3a, and Z are as defined herein, and which are inhibitors of Trk kinases and are useful in the treatment of pain, cancer, inflammation, neurodegenerative diseases and certain infectious diseases;intermediates of formula IIused in the preparation of compounds of formula I and the compound for use in the treatment of the diseases mentioned above of the following formula: