somwhat working refactored version