see added comments in code