working prototype, desperately needs refactoring