Bug Fixes and Code Cleanup