Properly cleanup when terminating server

Fixes #210
Merge pull request #211 from ebryn/cleanup

Properly cleanup when terminating server