a port of bastos to js :)