Code commented and some bug fixed