[RFC] Spliting two-iteration loops in ExpandBufnames()

( I made the changes that were mentioned in comments of #678. This new pull-request created so that git history does not clutter. )

Two 2-iteration loops (one inside the other) were present in ExpandBufnames() which have been split.
As in #572