rework main wait loop