Bug fixes and code improvements