big ol refactor. fixed race condition.