Refactored invocation of before and after methods and made execution safer