Extract shared code from handlers