firm the main background logic and make it robust