move duplicate code into separate function
move some code inside _X11 ifdef
Merge pull request #236 from s3erios/master

Some code simplification