refactorized sequential and also implemented sequential space saving version