cleaning and robustifying a little bit the code