implemented sanity checks, fixed bugs they exposed