ENG-6390 cleaned and rearranged the code