Moved button handling to separate function