refactor to make code a bit nicer