Properly cleanup when terminating server

Fixes #210