Finalize chg_dir and chg_mag code.
Add better error handling.