work on algorithm - not completely correct