Rearranged code to possible fix problems on github.