Refactoring part 3. Finished refactoring but still need more testing!