Lots of edge case fixes. Refactoring. Handles some simple loops now