added comments and explained the functions