Refactored parts of code