rearranged code. not tested. about to do that though