loop reimplemented with queue