Simplify implementation and reduce number of callback objects created.
Also added test cases.