Build fixes, and save/restore current device