Fixed an ugly error logging bug by moving scope around