Modified code to be more flexible