rearranged some code, added checking for check. still needs debugging though.