Isn't initialising with dirs.length - 1 redundant here?  We're going to assign files[0] in the loop below anyway.