Refactor it - made the code look nice. Removed tailrec. Very slow.