Finalized version, clean code.