Compiles and partially works, but crashes