Various bugs fixed