Close to final version of the code