Updated pseudo-code as per google hangout