final commented and working codes