#636 code refactor