Sanitize code and prepare match for tail recursion