fixing the task that will never run and hiding close link
converting some loop logic to sql