Use foldl instead of recursion in build