working correct on test-cases, gets segfault on j