TMDM-7434: code refactoring and clean-ups
(one more tricky (not easy to reproduced)bug related to 'close' to be fixed)