Add define method and refactor code