Rewrite all function selection functions

So they're less ugly. Also shorter.