add sanity checks and comments