Removed unused code; added a recursive algorithm