added some commenting to explain what's going on