Some bug fixes and minor changes