Added some comments and documentation to functions