Moved variables, removed redundant SQL