'match' updated with proper tail recursive approach