refactor logic and add optional basic auth