Added cleanup function to execute before stopping
MAX_LATENCY is in seconds, not ms