Updated version that uses structs