Updated working code - testing needed