Added EOF handling, assorted improvements.