fixed some errors, race condition still there