changed sort to work better possibly. also fix global and semicolons