Added code to clean dirty flags on write and reset. Aded tests to test it.