Fixed a number of bugs. Counts should be more accurate. Scripts should not crash.