tried to make code more flexible