working somehow, need refactoring