Updated comments and improved the code