Cleaning code and adding comments, as well as more checks.

Signed-off-by: David Brainard <brainard@psych.upenn.edu>