Added dirty flags, moved part of the implementation to a cpp file.