not crazy about the mutable variables, but they are local scope, so I guess it is okay.