Added callback functions and cleaned it a bit