Added comments and made the code a bit more explicit and, hopefully, more self-documenting.
Added a few comments