finished and finalized code