first workin version with very small N and L and wo memory management