Some cleanup and bug fixe