Fixed a error which happened rarely
Fixed casting problems