Refactored and reloacted some code