functionally complete, but needs refactoring