reorg code to make it easier to scan