works properly but needs refactoring