finished algorithms and did some refactoring