Some comments and renamed a variable to make things clearer.