Optimized and reduce the length of code and added with more comments