pls check typesafety of casts everywhere to turn segfaults into run-time errors