rough working version. time for refactoring