made more pedagogical, changed some variable names.