replaced duplicate code with functions, almost working