Fixed LaTeX errors, implemented a flag for inactive counter (still buggy), refactoring