As suggested by deviantintegral the flood API may be able to simplify some of this code.

Pros and cons around this solution will need to be investigated.