Fix for issue #25: Overwriting data correctly

Plus optimization.