fix bug and cleaned up code and add threshold field to count command