First working version of the code