Added lot of comments and made little refactoring