refactoring. still one major flaw.