as you are using $app in a couple of places I'd find it more readable to declare it once outside the if statement