Clean and refactore code