Added comments and fixed minor bugs