added more sanity checks, comments.