Fixed threshold logic. Added intermediate csv dump.