restart of branch, this is to include changes from trunk


[SVN r40545]
merged fixes from previous instance of branch (r38875:40542), addressing #856 and #586

[SVN r40550]
removed whitespace at the end of lines

[SVN r40552]
formatted code: removed whitespace at end of lines and added some empty lines to increase readability

[SVN r40689]
formatted code: added some empty lines to increase readability

[SVN r40690]
removed compiler warning about missing newline at end of file

[SVN r40691]
fixed signedness of variable and cleaned up whitespace

[SVN r40696]
Test case and possible fix for Issue #1002


[SVN r40700]