Imported changes by TCI/Dmitry Belyavsky