Refactor and comment code