Working  version of code