Major refactoring to use SessionFactory instead of Session everywhere.