several improves, don't open file twice