Added some comments and improved readability