refactor for easier reading and better testing
make #context compatible with previous versions