Adding selection support

The multiple commits are caused by me uploading code via Github. I will rebase it all before I merge. 
There’s missing whitespace all over the place, which makes the code really hard to read. Please use spaces before and after braces, and use blank lines between blocks.