(3_1_X) Fix 14424 and 14367

Backport of PR #126

[TIMOB-14424](https://jira.appcelerator.org/browse/TIMOB-14424)
[TIMOB-14367](https://jira.appcelerator.org/browse/TIMOB-14367)

Test using https://github.com/appcelerator/titanium_mobile/pull/4627 and https://github.com/appcelerator/titanium_mobile/pull/4628