I think your definition is not entirely correct.
A molecule is the smallest particle of a substance that retains the chemical and physical properties of the substance and can independently exist.
NOT ALL COMPOUNDS are molecules. For example, ionic compounds (e.g. NaCl) are NOT molecules because they actually exist as separate ions in solution transferring electrons. If you studied the model of ionic substances, you'll know they consist of positively charged and negatively charged ions formed by tranferring of electrons. These ions with opposing charges attract each other to form a lattice and hence they are dependent on each other to retain certain properties.
Molecules generally consist of non-metals often in covalent bonds. For example, all noble gases (e.g. Helium) and diatomic substances (e..g Oxygen) exist as molecules because they are the smallest unit of a substance that can independently exist. Carbon dioxide consists of molecules because each unit of CO2 is capable of separate existence since they are stable in an intramolecular covalent bond.