First working version of code