final version of the code