Moved around variable declarations to fix NullPointerException.