Added a callback function to make sure code is executed in the right order