optimized inSpace function