Clean and structure code from previous commit. Fix small bugs with maps sync.