Change filename format and add more error checking