Comments added and code made more beautiful