General bug fixes and improvements