cleaned up code, added nice ecmascript iterator, added some error-checking