Fixed problem with state-machine

Moving between modes wasn't handled correctly.

- putting 2 windows both in SLAVE-mode caused deadlock.
- killing MASTER caused SLAVE to run wild.
- added return-value-check