working prototype. refactoring needed.