Example implementation outline (pseudocode):