Several bugs fixed