Some refactoring and decomposition, fixed bugs with write event and some others.