Some refactoring/cleanup and functionality satisfying step 5