changed file checks to fstat instead of stat to avoid race conditions