replaced double for-loops by single for-loops