Refactor and improvements