fixed potential race conditions