added before and after hooks, plus general improvements