Refactored some parts of code