Refactor and fixes.