Major refactor and improvements