Fixed sanity check and added a few comments