I couldn't test this (I can't compile), but looking at everything it seems that if gainchance is <0 this would tell the DM a negative number of monsters to kill before the player can gain, so I hope this would fix that.