merged in kriskowal's CommonJS branch and Dmitry Baranovskiy's optimizations
(cherry picked from commit 4f783846de50e8de6d3001eb1ae23b9d041ccf23)