add sanity check and more random fixes