reorganize code and add basic block detection logic