Rearranged some things to make the code faster