adds callback function
fixes memory issue as well as re-orders somethings.