rearranging stuff a bit. this needs testing