Add flush() and close()

This is meant as a proof-of-concept implementation, there is definitely room for improvement.