Reorganized the code and added an example factory at the end.