added threshold key
clean up code a little