Refactored and reformated code