implement flood detection/prevention and clean up code a bit