Added lots of comments. Renamed variables for clarity.