Moved enter/exit handling into a separate class. Tests updated.