Optimized to use ArraySegments + added first tests