comments, sanity checks, some refactoring