refactoring,algorythm improvements, added methods.