Refactors; Also fixes potential for an inf loop; Better parameter handling