some rearranging of code