Rearranged code, more debugging