[trunk] Merge final patch from issue 171 with code simplification. also handles signe...