working version of code