Added comments and formatted code a little.