further optimized version