Why is my script causing the app to crash?