ported selector engine code from https://github.com/rasmusfl0e/amoeba/blob/222a4058c6ecb6e2fb3cd5fb430ff661c31d7e75/_amoeba.js