losely working code. testing needed