minor refactor and add comments