Removing code duplication thanks to @asturur