basic function complete; todo: optimization