Refactor Engine#handle_signal to avoid code duplication

Hi,

A little refactoring to avoid code duplication. I've also added a spec for it.

I hope you'll like it! :)