Remove unneeded cast, reduce duplication, move declarations to their first use.
Merge pull request #210 from Dmitry-Me/removeUnneededCast

Remove unneeded cast, reduce duplication, move declarations to their fir...