It doesn't implement the low/critical distinction, but that could be added in two or three lines and the code's clean and commented with novices in mind.
(And I apologize for the thread necromancy. This thread has a non-trivial Google presence so I thought it was worthwhile.)
]]>