Added comments and made small adjustments

The code is now much clearer on what it does