Refactored repetitive code to a singe function and moved it to a
separate file.