attempted to fix race conditions, but did not succeed :(