revised version, with three arrays