Fix multiple serious bugs in factory placement code.