fix resurrection issues, improve factory exit, overhaul algorithm that decides which factory to build