Some code correction and adding better comments