Bug fixes and Code Cleanup