restart of branch, this is to include changes from trunk

svn path=; revision=40545
merged fixes from previous instance of branch (r38875:40542), addressing #856 and #586

svn path=; revision=40550
removed whitespace at the end of lines

svn path=; revision=40552
formatted code: removed whitespace at end of lines and added some empty lines to increase readability

svn path=; revision=40689
formatted code: added some empty lines to increase readability

svn path=; revision=40690
removed compiler warning about missing newline at end of file

svn path=; revision=40691
fixed signedness of variable and cleaned up whitespace

svn path=; revision=40696
Test case and possible fix for Issue #1002

svn path=; revision=40700