Cleanup and bug detection in environment_free.

Fixes #28.