Refactoring and code cleanup, added missing references and built.