Cleanups and casts

I don't even know if it works