Fix several medium impact CID issues