Fixing bugs in span calculation