Simplify code - bail out early, vreak loops early, reorder checks and declarations.
Merge pull request #336 from Dmitry-Me/SimplifyCodeBreakLoopsEarly

Simplify code - bail out early, vreak loops early, reorder checks and declarations