Refactor signal handling