tweaked for loops inner-outer mapping