More comments explaining what's happening and some minor code optimization.