Tried to simplify the code, but now segfaults.