Help with my block placement system

I’ve been trying to make a building system for a while now, and I found a module that I could use as a base to help me do so. The only problem is that if I change the grid size or the block size, the entire system falls appart causing new blocks to be misaligned.

I managed to pinpoint the issue to this function

function f.mouseGridPosition(b)
	if f.mouseTarget() then
		local pos = f.mousePosition()
		local normal = f.mouseBlockSide()
		local offset = f.GridSize/2-((pos-f.mouseTarget().Position)*normal).magnitude
		local vectorOffset = Vector3.new(offset, offset, offset)*normal
		local blockPos = pos + vectorOffset + normal		
		return f.positionToGrid(blockPos, f.GridSize)
	else
		return f.positionToGrid(f.mousePosition(), f.GridSize)
	end
end

image

image

Any help would be appreciated.