Fix issue #85.

Also do some code clean up.