Added more comments and some minor improvements in code.