Rearranged code, currently broken.