Handle race condition

Rewrite some core queries to handle race conditions more effectively