refectoring code, added text detection