Moved file counter to function