Added state detection for history.  This may be prone to bugs and deserves testing.  This commit (and 97928e15dd6582a697664ab6daf408f2f491c57e) fixes #15