do a bunch of explicit casting to avoid overflow hopefully