Should have used lists:mapfoldl/3