Working version of code