switch to named functions for easier debugging and less indenting