Add support to multi window mode. Clean code and fix some functions to manage the active windows and detect close and focus