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