TDI-30715:fix TDI-30715 and TDI-30521 together and improved some code.