refactor and add loading dialog in the beginning