Nit: The if statements should have brackets to make the scoping more explicit. These chains of implicit scoping can be confusing.