this is a working version does not produce a segment fault