code refactor and comments