Minor code optimization in for-loops and by reusing variables