CommonJS compatibility + minor refactoring to ease testing