Crash fixed, but code is now ugly

I also need to include delete() for memory