Refactored base case logic.
Cleaned up code.