Seperated handle function from the rest for easier debugging.