Added comments and created separate functions to make program readable.