Fixed bugs identified by Marco