Move shebang

This is based on #1663 and consolidates all the shebang logic into one place.