Finalized second version of code