Bug and error fixes