reordering of loops and if blocks