finalized code, to be tested afternoon