fixed indexing issue with empty space logic