Refactored, new detection algorithm added (not yet working).