still needs refactoring but finished