move signal handling to a function