refactoring of bgdetector