refactor and comment code