Code refactoring

Started moving out protocol specific code to its own library.