Code tidy and refactor