Adjust tests and refactor detect-context