Final version of the code