Copy/pasted code is cleaned-up;
Proper methods are used instead of casts;
Most files are reformatted;
Merge pull request #252 from solomax/master

Copy/pasted code is cleaned-up;