added comments and function explanation.