Refactor midway. Still dealing with infinite loop bug