added remove space algorithm