refactor and remove excessive calls to `stat