added flush and end before relay OFF