Add sanity checks and clean up save_priorities code