Improved algorithm to find the next free space