slightly refactored and fixed some structures from race errors