Fixed various infinite loop errors, caused by invalid references. Still have way to go.