Fixing numerous bugs