working but needs refactoring