Refactor to make it more flexible