Added some comments and fixed code style in some places (not all)