code cleanup, various corrections, more accurate mirror detection