Update and refactor code