Fixes regressions when running on a cluster with more or fewer than 4 engines

This is based on @cowlicks PR #441.  Rebased on `master`.