Second attempt: much cleaner. Nearly working; it only needs to clean the fifos after reading them