Fixed a bug in threshold function