Minor bug fixes and code improvements