Working version of the code