added more commenting to make things clearer