Combined pulls #1113 and #1115

See #1113 and #1115  for details. This fixes all the threading issues I could find. It also adds a FactSystem unit test.