Updated code. Broken: generating segfault.