Fixing duplicate code