This merge request simplify some code and make it safer. This has the changes for E3-1211, E3-722 and E3-1248.