first working version, needs cleanup and refactoring