Added JSDoc comments and split the program into easy-to-understand functions.