Replaced some thread un-safe functions with their safe counterparts