Implement flexible target specification

See commit message.
This feature may want to remain unimplemented for now due to some open questions here, but it probably doesn't matter too much.