Moved duplicated code to a method