Catches up with #773, #779 and #781. Only the import blocks in command.rs and api/foreign.rs conflicted.