Various fixes (race conditions,bugs) and redundant types cleanup

Can't change this yet, as it is backwards incompatible.