Optimized functions - reduced number of for-loops