final commented and working version