Added some comments and code fixes