Code cleanup
Hopefully fixed a few crashes too