Finished coding factor functionality

Still some need to do: Improve the performance of creating the pivoting
table