update comments and reorg to make it easier to understand