Modified to support AMD, CommonJS, and global usage.