Several bugs fixed and improvements