Finally I made signals handling (more or less) working... Code cleanup and optimization