Amanda's pseudo code added