Bug fixes and code cleanup.

Improved variable initialization.