improving the code to make it work better with the async calls