improved before middleware support