Believe I've fixed it, by removing any calls to sleep() and making sure all data ends with double-newline.