refined the logic to debug